﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.BasicBase.BLL;
using Yipusoft.SoldierArchives.BLL;
using System.Web.UI.DataVisualization.Charting;
using System.Collections;
using System.Text;
using SmartWeb;
using System.Drawing;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;

public partial class Police_Question_AnswerAnlyse2 : System.Web.UI.Page
{
    private string[] charts = { "Column", "Spline", "StepLine", "Spline" };

    QuestionDataContext db = new QuestionDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            init();
        }
    }

    private void init()
    {
        if (Request["departmentID"] == null)
        {
            Response.Write("<script>alert('没有权限！');history.back(0);</script>");
            return;
        }
        else
        {
            DepartmentInfo department = Yipusoft.Account.BLL.Department.GetDepartment(Request["departmentID"]);
            lblCaptin.Text = department.ShortName + "详细情况查看";
        }


        string departmentid = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (Request.QueryString["ID"] != null)
        {
            ///////---------------------------------------------///
            List<problem> problems = new List<problem>();
            
            Guid sid = new Guid(Request["ID"]);
            List<SecurityProblem> problemList = db.SecurityProblem.Where(o => o.Sid == new Guid(Request.QueryString["ID"])).ToList();


            for (int i = 0; i < problemList.Count; i++)
            {
                problem pm = new problem();
                string c = SmartWeb.BLL.UserFun.getMarker(i + 1);
                pm.No = c;
                SecurityProblem problem = problemList[i];
               

                int sodierCount = GetSodierCount(sid, problem.Id);
                
                pm.SodierCount = sodierCount;
                pm.Name = problemList[i].Title;

                decimal age = 0;
                decimal sum = (sodierCount * getMaxMark(problem));
                if (sum == 0)
                {
                    pm.age = 0;
                    pm.state = 3;

                    UserChart.Series["未超出警戒线"].Points.AddXY(c, DBNull.Value);
                    UserChart.Series["警戒线"].Points.Add((double)problem.GuardCount / 100);
                }
                else
                {
                    age = GetSumScore(sid, problem.Id) / sum;
                    pm.age = age;
                    UserChart.Series["未超出警戒线"].Points.AddXY(c, age);
                    UserChart.Series["警戒线"].Points.Add((double)problem.GuardCount / 100);
                }





                UserChart.ChartAreas["Chart1"].AxisX.IsLabelAutoFit = false;
                UserChart.Series["未超出警戒线"].LabelFormat = "0.0" + "%";

                if (problem.GuardCount / 100 <= age)
                {
                    pm.state = 1;
                    UserChart.Series["未超出警戒线"].Points[i].Color = Color.FromArgb(200, 0, 0);
                }
                else if (pm.state != 3)
                {
                    pm.state = 2;
                }
                problems.Add(pm);

            }


            SecuritySubject subject = db.SecuritySubject.Where(o => o.Id == sid).FirstOrDefault<SecuritySubject>();
            UserChart.Titles[0].Text = subject.Caption + "--百分比";




            UserChart.DataManipulator.Sort(PointSortOrder.Ascending, "X", "未超出警戒线");
            UserChart.DataManipulator.Sort(PointSortOrder.Descending, "Y", "未超出警戒线");

            List<problem> ps = problems.Where(o => o.state == 1).OrderBy(o => o.No).OrderByDescending(o => o.age).ToList();
            DataList1.DataSource = ps;
            DataList1.DataBind();
            DataList1.Caption = "<span style='color:#C80000'>超出警戒线</span>";

            DataList2.DataSource = problems.Where(o => o.state == 2).OrderByDescending(o => o.age).OrderBy(o => o.No).OrderByDescending(o => o.age).ToList();
            DataList2.DataBind();
            DataList2.Caption = "<span style='color:#418CF0'>未超出警戒线</span>";

            DataList3.DataSource = problems.Where(o => o.state == 3).OrderByDescending(o => o.age).OrderBy(o => o.No).OrderByDescending(o => o.age).ToList();
            DataList3.DataBind();

        }

    }

    /// <summary>
    /// User defined sorting order logic
    /// </summary>
    public class MyComparer : IComparer
    {
        // This function should return a value less than zero if the first object is less than the second,
        //  zero if they are equal and a value greater than zero if the first object is greater than the second
        public int Compare(object a, object b)
        {
            DataPoint pointA = (DataPoint)a;
            DataPoint pointB = (DataPoint)b;

            // Compare axis labels first
            int result = pointA.AxisLabel.CompareTo(pointB.AxisLabel);

            // If axis labels are equal - compare Y values
            if (result == 0)
            {
                result = pointA.YValues[0].CompareTo(pointB.YValues[0]);
            }

            return result;
        }

        #region IComparer 成员

        int IComparer.Compare(object x, object y)
        {
            throw new NotImplementedException();
        }

        #endregion
    }


    public int GetSodierCount(Guid Sid, Guid Pid)
    {
        string Departmentid = Request["departmentID"];
        QuestionDataContext db = new QuestionDataContext();
        int pp;

        pp = (from cc in db.SecurityResult where cc.Sid == Sid && cc.Pid == Pid && cc.SetDepartment == Departmentid group cc by cc.SoldierID into aa select aa).Count();
        return pp;
    }

    public decimal GetSumScore(Guid sid, Guid pid)
    {
        string Departmentid = Request["departmentID"];
        decimal? pp = 0;
        QuestionDataContext db = new QuestionDataContext();

        pp = (from aa in db.SecurityResult where aa.Sid == sid && aa.Pid == pid && aa.SetDepartment == Departmentid select aa.Score).Sum();





        if (pp.HasValue)
        {
            return pp.Value;
        }
        else
        {
            return 0;
        }
    }

    public decimal getMaxMark(SecurityProblem problem)
    {
        decimal? result = 0;
        if (problem.IsMultiple == 1)
        {
            var pp = (from aa in db.SecurityOption where aa.PId == problem.Id select aa.Mark).Sum();
            result = pp;
        }
        else
        {
            var pp = (from aa in db.SecurityOption where aa.PId == problem.Id select aa.Mark).Max();
            result = pp;
        }
        if (result.HasValue)
        {
            return result.Value;
        }
        else
        {
            return 0;
        }
    }


    //protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    //{

    //    if (e.Row.RowType == DataControlRowType.DataRow)
    //    {
    //        string c = SmartWeb.BLL.UserFun.getMarker(e.Row.RowIndex + 1);
    //        SecurityProblem problem = e.Row.DataItem as SecurityProblem;
    //        Label lblTitle = (Label)e.Row.FindControl("lblTitle");
    //        lblTitle.Text = c.ToString() + "、" + problem.Title;
    //        //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
    //        e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
    //        //当鼠标离开的时候 将背景颜色还原的以前的颜色 
    //        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
    //    }
    //}


    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            problem problem = e.Item.DataItem as problem;
            Label lblTitle = (Label)e.Item.FindControl("lblTitle");
            lblTitle.Text = problem.No.ToString() + "、" + problem.Name + "&nbsp;&nbsp;(答题人数：" + problem.SodierCount + ")";
    
        }
    }

    class problem
    {
        public string Name;
        public string No;
        public int SodierCount;
        public int state;
        public decimal age;
    }
}
